package com.xiaoyu.lanling.feature.moment.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.opensource.svgaplayer.SVGADrawable
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.RxUtils
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.data.SvgaData
import com.xiaoyu.lanling.event.moment.MomentAccostEvent
import com.xiaoyu.lanling.feature.moment.controller.RecommendMomentController
import com.xiaoyu.lanling.feature.moment.data.MomentRecommendListData
import com.xiaoyu.lanling.view.OnShowFragment
import com.xiaoyu.lanling.widget.svga.SvgaBaseCallback
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_moment_detail.*
import kotlinx.android.synthetic.main.fragment_reccomend_moment.*
import kotlinx.android.synthetic.main.fragment_reccomend_moment.accost_svga
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class RecommendMomentFragment : OnShowFragment() {

    private lateinit var momentController: RecommendMomentController
    private var mSvgaDisposable: Disposable? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutInflater.inflate(R.layout.fragment_reccomend_moment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        initView()
        initData()
        initBind()
        initEvent()
    }

    private fun initView() {
        recyclerview.itemAnimator = null
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: MomentAccostEvent) {
                showAccostSvga()
            }
        })
    }

    private fun initBind() {
        accost_svga?.callback = object : SvgaBaseCallback() {
            override fun onFinished() {
                accost_svga?.visibility = View.GONE
            }
        }
    }

    private fun initData() {
        val listData = MomentRecommendListData()
        activity?.let {
            momentController = RecommendMomentController.create(it, listData, swipe_refresh_layout, recyclerview)
            momentController.loadFirstPageData()
        }
    }

    private fun showAccostSvga() {
        accost.isSelected = true
        mSvgaDisposable?.dispose()
        mSvgaDisposable = SvgaData.createTaskFromAsset("accost.svga")
                .compose(RxUtils.ioToMainSingleScheduler())
                .subscribe({ entity ->
                    val drawable = SVGADrawable(entity)
                    accost_svga?.visibility = View.VISIBLE
                    accost_svga?.setImageDrawable(drawable)
                    accost_svga?.loops = 1
                    accost_svga?.startAnimation()
                    mSvgaDisposable?.dispose()
                }, { t -> t.printStackTrace() })
    }

    override fun onDetach() {
        super.onDetach()
        RxUtils.disposable(mSvgaDisposable)
    }
}